home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 169 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  61 lines

  1. Path: news.mel.aone.net.au!usenet
  2. From: clyde@hitech.com.au (Clyde Smith-Stubbs)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help about structure
  5. Date: Tue, 02 Jan 1996 22:09:53 GMT
  6. Organization: HI-TECH Software
  7. Message-ID: <30e9ac58.1078188864@news.bne.aone.net.au>
  8. References: <4c3t9q$aob@chleuasme.francenet.fr>
  9. Reply-To: clyde@hitech.com.au
  10. NNTP-Posting-Host: skyhawk.hitech.com.au
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. On 30 Dec 1995 17:35:54 GMT, jo <jobloch@micronet.fr> wrote:
  14.  
  15. >How can I translate from Pascal to C such a structure:
  16. >token=record;
  17. >    begin
  18. >         nature:tokennature;
  19. >         case nature : tokennature of
  20. >                tkinteger: (i:integer);
  21. >                tkreal: (r:double);
  22. >                ...
  23. >    end;
  24. >
  25.  
  26. Like this:
  27.  
  28. struct token
  29. {
  30.     enum {tkinteger, tkreal }    nature;
  31.     union {
  32.         int    i;
  33.         double    r;
  34.     }    tk_u;
  35. }    token;
  36.  
  37. You would subsequently have code like:
  38.  
  39.     switch(token.nature) {
  40.  
  41.     case tkinteger:
  42.         printf("Int value is %d\n", token.tk_u.i);
  43.         break;
  44.  
  45.     case tkreal:
  46.         printf("Float value is %f\n", token.tk_u.r);
  47.         break;
  48.     }
  49.  
  50. but note that it's up to you to make sure that you maintain the
  51. relationship between the "nature" value and what is stored in the
  52. union - there is no run-time check.
  53.  
  54.  
  55.  Clyde Smith-Stubbs       | HI-TECH Software,       | Voice: +61 7 3300 5011
  56.  clyde@hitech.com.au      | P.O. Box 103, Alderley, | Fax:   +61 7 3300 5246
  57. http://www.hitech.com.au  | QLD, 4051, AUSTRALIA.   | BBS:   +61 7 3300 5235
  58. ----------------------------------------------------------------------------
  59. FREE! Download our shareware (FREE for noncommercial use) MS-DOS C Compiler!
  60.              Point your Web browser at http://www.hitech.com.au/
  61.